package com.recharge.api.controller;

import com.alibaba.fastjson.JSONArray;
import com.recharge.api.dto.request.CallBackRequest;
import com.recharge.api.dto.request.TransactionRequest;
import com.recharge.api.dto.response.BalanceResponse;
import com.recharge.api.dto.response.OperatorResponse;
import com.recharge.api.dto.response.StatusResponse;
import com.recharge.api.dto.response.TransactionResponse;
import com.recharge.api.service.ApiService;
import com.recharge.api.service.TransactionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api")
public class ApiController {

    @Autowired
    private ApiService apiService;

    @GetMapping("/db-test")
    public String testDbConnection() {
        try {
            // Add simple JPA query here if needed
            return "Database connection successful";
        } catch (Exception e) {
            return "Database connection failed: " + e.getMessage();
        }
    }

    /**
     * Balance API
     * @param userId
     * @param token
     * @return
     */
    @PostMapping("/balance")
    public BalanceResponse getBalance(@RequestParam String userId, @RequestParam String token, HttpServletRequest request) {
        log.info("Received balance request：userId={}, token={}", userId, token);
        return apiService.getBalance(userId, token);
    }

    /**
     * Transaction API
     * @param request
     * @return
     */
    @PostMapping("/processTransaction")
    public TransactionResponse processTransaction(@RequestParam TransactionRequest request) {
        log.info("Get balance request: request={}", request);
        return apiService.processTransaction(request);
    }

    /**
     * Transaction Status API
     * @param userid
     * @param token
     * @param transid
     * @return
     */
    @PostMapping("/getTransactionStatus")
    public StatusResponse getTransactionStatus(@RequestParam String userid, @RequestParam String token, @RequestParam String transid) {
        log.info("Transaction Status API: userid={}, token={}, transid{}", userid, token, transid);
        return apiService.getTransactionStatus(userid, token, transid);
    }


    /**
     * OperatorCode List
     * @return
     */
    @PostMapping("/getAllOperators")
    public JSONArray getAllOperators() {
        log.info("getAllOperators");
        return apiService.getAllOperators();
    }

    @PostMapping("/callBackResponse")
    public String callBackResponse(@RequestParam CallBackRequest callBackRequest) {
        log.info("CallBack Response request：callBackRequest={}", callBackRequest);
        return apiService.callBackResponse(callBackRequest);
    }




}